package com.cskaoyan.controller.order;

import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.order.OrderListBo;
import com.cskaoyan.model.bo.order.OrderShipBo;
import com.cskaoyan.model.vo.BaseData;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.order.OrderChannelVo;
import com.cskaoyan.model.vo.order.OrderDetailVo;
import com.cskaoyan.service.order.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

/**
 * @Author: mjm
 * @Date: 2021/11/30
 * @Time: 15:57
 * @Description:
 */
@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * @description: 显示全部订单信息
     * @param: PageBo
     * @return:
     */
    @RequiresPermissions("admin:order:list")
    @GetMapping("list")
    public BaseRespVo orderList(OrderListBo orderListBo, PageBo pageBo) {
        BaseData baseData = orderService.orderList(orderListBo, pageBo);
        return BaseRespVo.ok(baseData);
    }

    /**
     * @description: 显示订单细节
     * @param: id
     * @return:
     */
    @RequiresPermissions("admin:order:detail")
    @GetMapping("detail")
    public BaseRespVo orderDetail(Integer id) {
        OrderDetailVo orderDetailVo = orderService.orderDetail(id);
        return BaseRespVo.ok(orderDetailVo);
    }

    /**
     * @description: 退款
     * @param: orderId: 2
     * refundMoney: 459
     * @return:
     * errmsg: "订单退款失败"
     * errno: 621
     */
    @RequiresPermissions("admin:order:refund")
    @PostMapping ("refund")
    public BaseRespVo orderRefund(@RequestBody Map<String,String> map) {
        Integer id = Integer.parseInt(map.get("orderId")) ;
        BigDecimal refundMoney = BigDecimal.valueOf(Double.parseDouble(map.get("refundMoney"))) ;

        int affectedRows = orderService.orderRefund(id, refundMoney);
        if (affectedRows != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.manual(621,"订单退款失败",null);
    }


    /**
     * @description: 发货
     * @param:
     * orderId: 4
     * shipChannel: "ZT"
     * shipSn: "1"
     * @return:
     */
    @RequiresPermissions("admin:order:ship")
    @PostMapping("ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBo orderShipBo) {
        int affectedRows = orderService.orderShip(orderShipBo);
        if (affectedRows != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }


    /**
     * @description: 删除订单，默认订单不能删除
     * @param: orderId: 11
     * @return: {"errno":623,"errmsg":"订单不能删除"}
     */
    @RequiresPermissions("admin:order:delete")
    @PostMapping("delete")
    public BaseRespVo orderDelete(Integer orderId) {
        return BaseRespVo.manual(623,"订单不能删除",null);
    }


    /**
     * @description: 获取物流信息
     * @param: 无
     * @return:
     */
    @GetMapping("channel")
    public BaseRespVo orderChannel() {
        ArrayList<OrderChannelVo> orderChannelVos = orderService.orderChannel();
        return BaseRespVo.ok(orderChannelVos);

    }
}
